package com.ang.jdk8.stream;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ListToMap {
    public static void main(String[] args) {
        ListToMap listToMap= new ListToMap();

        listToMap.test0();
    }

    public void test0(){
        List<User> list = getList();
        Map<String,String> map1 = list.stream().collect(Collectors.toMap(u->u.id, User::getName, (k1, k2)->k2));
        System.out.println(JSON.toJSONString(map1));

        System.out.println("-------------------------");
        Map<String,User> map2 = list.stream().collect(Collectors.toMap(User::getId, Function.identity(), (k1,k2)->k1));
        System.out.println(JSON.toJSONString(map2));
    }

    public List<User> getList(){
        List list = new ArrayList();
        IntStream.range(1,10).forEach(i->{
            User u = new User(i+"","test"+i);
            list.add(u);
        });
        return list;
    }

    @Data
    @AllArgsConstructor
    class User{
        private String id;
        private String name;
    }
}
